﻿using MyIServices;
using MyModels.Models;
using MyRepository;
using MyRepository.BASE;
using MyServices.BASE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyServices
{
    public class RoleModulePermissionService:BaseService<RoleModulePermission>,IRoleModulePermissionService
    {
        private readonly IRoleModulePermissionRepository _dal;
        private readonly IBaseRepository<Modules> _moduleRepository;
        private readonly IBaseRepository<Role> _roleRepository;

        public RoleModulePermissionService(
            IRoleModulePermissionRepository dal,
            IBaseRepository<Modules> moduleRepository,
            IBaseRepository<Role> roleRepository)
        {
            _dal = dal;
            _moduleRepository = moduleRepository;
            _roleRepository = roleRepository;
        }

        public async Task<List<RoleModulePermission>> GetRMPMaps()
        {
           return await _dal.GetRMPMaps();
        }

        public async Task<List<RoleModulePermission>> GetRoleModule()
        {
            var roleModulePermission = await Query(a => a.IsDeleted == false);
            var roles = await _roleRepository.Query(i=>i.IsDeleted == false);
            var modules = await _moduleRepository.Query(a=>a.IsDeleted == false);
            if (roleModulePermission.Count > 0) { 
                foreach(var item in roleModulePermission)
                {
                    item.Role = roles.FirstOrDefault(i => i.Id == item.RoleId)!;
                    item.Modules = modules.FirstOrDefault(i => i.Id == item.ModuleId)!;
                }
            }
            return roleModulePermission;
        }

        public async Task<List<RoleModulePermission>> RoleModuleMap()
        {
            return await _dal.RoleModuleMaps();
        }

        public async Task UpdateModuleId(long permissionId, long moduleId)
        {
            await _dal.UpdateModuleId(permissionId, moduleId);
        }
    }
}
